//
//  WOInfoViewController.m
//
//  Created by Olof Millberg on 2010-03-25.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "WOInfoViewController.h"

// Table view sections
#import "WOAddressSection.h"
#import "WOTextSection.h"
#import "WOStatusSection.h"
#import "WOContactSection.h"

// Table view cells
#import "AddContactCell.h"
#import "AddressCell.h"
#import "ContactCell.h"
#import "TextCell.h"
#import "StatusCell.h"
#import "WOPerson.h"

@implementation WOInfoViewController

#pragma mark Initialization Methods

/* Initialization */
- (id) init
{
	if (self = [super initWithStyle:UITableViewStyleGrouped])
	{
		self.title = @"Info";
		UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Info" image:[UIImage imageNamed:@"tabbar_info.png"] tag:1];
		self.tabBarItem = theItem;
		[theItem release];
	}
	return self;
}

- (void) viewDidAppear:(BOOL)animated
{
	[super viewDidAppear:animated];
	self.tabBarController.title = self.title;
	[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"currentTab"];
}

- (void) dealloc
{
	[sections release];
	[super dealloc];
}

- (void) loadView
{
	[super loadView];
	
	self.tableView.rowHeight = 64;
	
	self.sections = [NSMutableArray array];
	[self.sections addObject:[WOAddressSection addressSectionWithTitle:[NSString stringWithFormat:@"%i", workOrder.workID]]];
	[self.sections addObject:[WOTextSection textSectionWithTitle:@"" text:workOrder.description]];
	[self.sections addObject:[WOStatusSection statusSection]];
	[self.sections addObject:[WOContactSection contactSectionWithTitle:@"Kontaktpersoner" 
													  contactList:workOrder.contactPersons 
														 editable:NO]];
	[self.sections addObject:[WOContactSection contactSectionWithTitle:@"Arbetsledare" 
													  contactList:workOrder.workLeaders 
														 editable:NO]];
	[self.sections addObject:[WOContactSection contactSectionWithTitle:@"Tilldelade" 
													  contactList:workOrder.employees 
														 editable:YES]];
}

#pragma mark -

#pragma mark UITableView Methods

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [super tableView:aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath];
	
	// Handle cells that aren't WOTableViewCells or that need delegate and such set manually.
	
	//Description.
	if ([cell isKindOfClass:[TextCell class]])
		((TextCell*)cell).text = self.workOrder.description;
	//Contacts.
	else if ([cell isKindOfClass:[ContactCell class]])
	{
		NSMutableArray *contactList = ((WOContactSection *)[sections objectAtIndex:indexPath.section]).contactList;
		WOPerson *contactPerson = [contactList objectAtIndex:indexPath.row];
		
		((ContactCell*)cell).contactPerson = contactPerson;
		((ContactCell*)cell).editable = NO;
		((ContactCell*)cell).viewController = self;
	}
	//Add contact
	else if ([cell isKindOfClass:[AddContactCell class]])
	{
		((AddContactCell *) cell).delegate = self;
		((AddContactCell *) cell).viewController = self;
	}
	
	return cell;
}

#pragma mark -
				 
#pragma mark People Picker Delegate Methods
			
- (void) addContactCell:(AddContactCell *)addContactCell didSelectPerson:(WOPerson *)person
{
	if(![self doesEmployee:person existIn:self.workOrder.employees])
	{
		[self.workOrder.employees addObject:person];
	}
	[self.tableView reloadData];
}
				 
#pragma mark -

#pragma mark Checker Methods

- (BOOL) doesEmployee:(WOPerson *) aPerson existIn:(NSMutableArray *) someEmployees
{
	BOOL exists = NO;
	for(WOPerson *currentPerson in someEmployees)
	{
		if([currentPerson.socialID isEqualToString: aPerson.socialID])
		{
			exists = YES;
			break;
		}
	}
	return exists;
}

#pragma mark -

@end
